cmake_minimum_required(VERSION 3.8)
project(rms_pkg)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()
cmake_policy(SET CMP0071 NEW)

# 查找依赖项
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core Gui Qml Quick Multimedia MultimediaWidgets Sql QuickControls2)
find_package(Protobuf REQUIRED)   # Protobuf
find_package(rclcpp_action REQUIRED)

# ROS 2消息生成相关
find_package(rosidl_typesupport_cpp REQUIRED)
find_package(rosidl_generator_c REQUIRED)

# 添加spdlog 依赖项
find_package(spdlog REQUIRED)
find_package(fmt REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# 生成消息接口（使用msg目标生成方式）
rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/Heartbeat.msg"
  "msg/SensorData.msg"
  "action/TaskExecution.action"
  "action/MapDownload.action"
  "srv/RobotStatus.srv"
  DEPENDENCIES std_msgs geometry_msgs
)

# 获取消息类型支持目标（必须在 add_executable 后调用）
rosidl_get_typesupport_target(
  typesupport_target 
  ${PROJECT_NAME} 
  "rosidl_typesupport_cpp"
)

include_directories(
  include
  include/seer
  src
  ${PROJECT_SOURCE_DIR}/hikvision_sdk/include   # 添加海康SDK的 include 路径
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
)

# 定义可执行文件目标
add_executable(RMS
  qml/qml.qrc
  resources/resources.qrc
  main.cpp
  include/heartbeat_model.cpp
  include/heartbeat_listener.cpp
  include/ros_worker.cpp
  include/hik_camera_controller.cpp
  include/mapdata.cpp
  include/log_manager.cpp
  include/file_loader.cpp
  include/message_map.pb.cc
  include/seer/SCStatusTcp.cpp
  include/seer/RequestManager.cpp
  include/seer/RobotController.cpp
  include/seer/StatusWorker.cpp
  src/sensor_data_subscriber.cpp
  #src/map_download_action_client.cpp
  #src/robot_status_client.cpp
  include/db/database_manager.cpp
  src/task_action_client.cpp
  src/task_client_factory.h
)

# 添加海康SDK的 lib 路径
link_directories(${PROJECT_SOURCE_DIR}/hikvision_sdk/lib)

# 链接 SDK 动态库（假设是 libhcnetsdk.so）
#target_link_libraries(RMS hcnetsdk)
# 添加ROS 2依赖
ament_target_dependencies(RMS
  rclcpp
  std_msgs
  geometry_msgs
  rclcpp_action
)

# 添加头文件包含目录
target_include_directories(RMS PRIVATE
  include
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  ${Qt5Qml_INCLUDE_DIRS}
  ${Qt5Quick_INCLUDE_DIRS}
)

# 链接库（确保在 add_executable 后）
target_link_libraries(RMS
  ${typesupport_target}
  Qt5::Core
  Qt5::Gui
  Qt5::Qml
  Qt5::Quick
  Qt5::Multimedia
  Qt5::MultimediaWidgets
  Qt5::Sql
  Qt5::QuickControls2
  spdlog::spdlog
  fmt::fmt
  rclcpp_action::rclcpp_action
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/libhcnetsdk.so
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/libPlayCtrl.so
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/libAudioRender.so
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/libSuperRender.so
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/libHCCore.so
  # 其他依赖库（根据实际报错添加）
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/HCNetSDKCom/libHCCoreDevCfg.so
  ${CMAKE_SOURCE_DIR}/hikvision_sdk/lib/HCNetSDKCom/libStreamTransClient.so
  ${PROTOBUF_LIBRARIES}
)

# 安装目标
install(TARGETS RMS
  DESTINATION lib/${PROJECT_NAME}
)

# 测试配置
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_export_dependencies(rosidl_default_runtime)
ament_package()
